package com.jinunn.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jinunn.system.common.result.Result;
import com.jinunn.system.model.params.UpdatePwdParam;
import com.jinunn.system.model.params.UpdateUserStatusParam;
import com.jinunn.system.model.params.UserParam;
import com.jinunn.system.model.vo.user.UserDetailVo;
import com.jinunn.system.model.vo.user.UserInfoVO;
import com.jinunn.system.model.vo.user.UserPageQuery;
import com.jinunn.system.model.vo.user.UserPageVO;
import com.jinunn.system.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author JIN
 * @since 2024-07-10
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/api/v1/user")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class UserController {

    private final UserService userService;

    @Operation(summary = "新增用户")
    @PostMapping("register")
    @PreAuthorize("@ss.hasPerm('sys:user:register')")
    public Result<Boolean> saveUser(@RequestBody @Validated UserParam userParam) {
        boolean result = userService.saveUser(userParam);
        return Result.judge(result);
    }

    @Operation(summary = "修改密码")
    @PostMapping(value = "/update/password")
    @PreAuthorize("@ss.hasPerm('sys:user:password:reset')")
    public Result<Boolean> updatePassword(@RequestBody @Validated UpdatePwdParam param) {
        boolean result = userService.updatePassword(param);
        return Result.judge(result);
    }

    @Operation(summary = "修改用户状态")
    @PostMapping(value = "/update/status")
    public Result<Boolean> updateUserStatus(@RequestBody @Validated UpdateUserStatusParam param) {
        boolean result = userService.updateUserStatus(param);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPerm('sys:user:delete')")
    public Result<Boolean> deleteUser(@Parameter(description = "用户ID") @PathVariable String id) {
        boolean result = userService.deleteUser(id);
        return Result.judge(result);
    }

    @Operation(summary = "用户表单数据")
    @GetMapping("/detail/{userId}")
    public Result<UserDetailVo> getUserForm(@Parameter(description = "用户ID") @PathVariable Long userId) {
        UserDetailVo formData = userService.getUserDetail(userId);
        return Result.success(formData);
    }

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/me")
    public Result<UserInfoVO> getCurrentUserInfo() {
        UserInfoVO userInfoVO = userService.getCurrentUserInfo();
        return Result.success(userInfoVO);
    }

    @Operation(summary = "用户分页列表")
    @PostMapping("/page")
    public Result<IPage<UserPageVO>> listPagedUsers(@RequestBody UserPageQuery queryParams) {
        IPage<UserPageVO> result = userService.listPagedUsers(queryParams);
        return Result.success(result);
    }
}
